package com.example.redistest.redispackage;

import com.example.redistest.redispackage.testclass.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * \* @author: wky
 * \* Date: 2022/10/08
 * \* Time: 18:19
 * \* To change this template use File | Settings | File Templates.
 * \* Description:RedisTest
 * \
 */
//@Component
public class RedisTest {

    @Autowired
    RedisCache redisCache;

    @PostConstruct
    private void init() {

        writeHashTest();
        test();
        writeObjectTest();
        pipelinedTest();
    }

    private  void test() {

        redisCache.setCacheObject("wky1","wky1wky1",30, TimeUnit.SECONDS);

        String str = redisCache.getCacheObject("wky1");

        System.out.println(str);
    }

    private void writeObjectTest() {
        redisCache.setCacheObject("user",new SysUser("niren01",10,100.01),
                5,TimeUnit.MINUTES);

        SysUser user = redisCache.getCacheObject("user");

        System.out.println("name:" + user.getName());
        System.out.println("age:" + user.getAge());
        System.out.println("balance:" + user.getBalance());
    }

    private void writeHashTest() {
        Map<String,SysUser> map = new HashMap<>();
        map.put("user1",new SysUser("user1",1,1.01));
        map.put("user2",new SysUser("user2",1,1.01));
        map.put("user3",new SysUser("user3",1,1.01));
        map.put("user4",new SysUser("user4",1,1.01));
        map.put("user5",new SysUser("user5",1,1.01));
        map.put("user6",new SysUser("user6",1,1.01));
        map.put("user7",new SysUser("user7",1,1.01));
        map.put("user8",new SysUser("user8",1,1.01));
        map.put("user9",new SysUser("user9",1,1.01));
        map.put("user10",new SysUser("user10",1,1.01));

        redisCache.setCacheMap("userx",map);

        Map<String,SysUser> map1 = redisCache.getCacheMap("userx");

        redisCache.setCacheMapValue("userx","user11",new SysUser("user11",1,1.11));

        SysUser user = redisCache.getCacheMapValue("userx","user8");
        System.out.println("读出的用户名为:" + user.getName());

        Map<String,SysUser> map2 = redisCache.getCacheMap("userx");

        user = redisCache.getCacheMapValue("userx","user6");
        System.out.println("读出的用户名为:" + user.getName());

        Map<String,SysUser> map3 = redisCache.getCacheMap("userx");
    }

    private void pipelinedTest() {

        Map<String,SysUser> userMap = new HashMap<>();
        for(int i=1;i<=100000;i++){
            String key = "user_" + i;
            userMap.put(key,new SysUser(key,i,(double)i));
        }
        redisCache.executePipelinedWrite(userMap);

        List<String> keyList = new ArrayList<String>(userMap.keySet());
        Map<String,SysUser> map = redisCache.executePipelinedRead(keyList);
        System.out.println("读出的数据个数为:" + map.size());

        //List<SysUser> users = new ArrayList<SysUser>(map.values());
        int i = 0;
        Set<String> keys = map.keySet();
        for(String key:keys) {
            System.out.println("key:" + key + " -- value:" + map.get(key).getName());
            i++;
            if(i>=10) break;
        }
        redisCache.deleteObject(keyList);
    }

}